package ar.uba.fi.RSACryptoFiuba.model.encrypt;

import ar.uba.fi.RSACryptoFiuba.model.InvalidValueException;
import ar.uba.fi.RSACryptoFiuba.model.RSACalculator;
import ar.uba.fi.RSACryptoFiuba.model.RSAMessage;
import ar.uba.fi.RSACryptoFiuba.model.RSAStep;

public class GenerateNumericalRepresentationStep extends RSAStep {
	
	@Override
	public void execute(RSAMessage message) {
		RSAEncryptMessage encryptMessage = (RSAEncryptMessage) message;
		String m = RSACalculator.getASCIIRepresentation(encryptMessage.getPlainText());
		encryptMessage.setNumericalRepresentation(m);
		
		encryptMessage.update();
	}
	
	@Override
	public void undo(RSAMessage message) {
		RSAEncryptMessage encryptMessage = (RSAEncryptMessage) message;
		encryptMessage.setEncryptedText("");
		encryptMessage.setNumericalRepresentation("");
		encryptMessage.setRemainNumericalRepresentation("");
		
		encryptMessage.update();
	}

	@Override
	public void test(RSAMessage message) {
		RSAEncryptMessage encryptMessage = (RSAEncryptMessage) message;
		String element = encryptMessage.getElementToEvaluate();
		String m = RSACalculator.getASCIIRepresentation(encryptMessage.getPlainText());
		boolean isValid = m.equals(element);
		if(!isValid){
			throw new InvalidValueException("El valor debe ser la representación en ASCII del Mensaje.");
		}
		encryptMessage.setNumericalRepresentation(element);
		
		encryptMessage.update();
	}

}
